package cn.xt.controller.orders;

import cn.xt.bean.*;
import cn.xt.service.house.CityService;
import cn.xt.service.house.FimgService;
import cn.xt.service.house.HouseService;
import cn.xt.service.orders.OrdersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author shkstart
 * @date 2018/11/26-9:29
 */
@Controller
@RequestMapping("/orders")
public class ControlOrders {
    @Resource(name = "ordersService")
    private OrdersService ordersService;

    @Resource(name = "houseService")
    private HouseService houseService;

    @Resource(name = "cityService")
    private CityService cityService;

    @Resource(name = "fimgService")
    private FimgService fimgService;

    @RequestMapping("/rent-order.html")
    public String rentOrder(HttpSession session, Model model){
        //获取id
        Users user = (Users) session.getAttribute("user");
        List<Orders> list = ordersService.listOrders(user.getUsers_id(),1);
        for (Orders list1:list){
            if(list1.getFirstday().getTime()>new Date().getTime()){
                model.addAttribute("qdate","true");
            }else{
                model.addAttribute("qdate","false");
            }
        }
        //保存全部订单
        model.addAttribute("qOrders",ordersService.listOrders(user.getUsers_id(),3));
        return  "rent-order";
    }

    @RequestMapping("/w-order.html")
    public String wOrder(HttpSession session, Model model){
        //获取id
         Users user = (Users) session.getAttribute("user");
        //未支付订单
        model.addAttribute("wOrders",ordersService.listOrders(user.getUsers_id(),0));
        return  "w-order";
    }

    @RequestMapping("/y-order.html")
    public String yOrder(HttpSession session, Model model){
        //获取id
         Users user = (Users) session.getAttribute("user");
        System.out.println("user"+user);
        //已支付
        List<Orders> list = ordersService.listOrders(user.getUsers_id(),1);
        for (Orders list1:list){
            if(list1.getFirstday().getTime()>new Date().getTime()){
                model.addAttribute("qdate","true");
            }else{
                model.addAttribute("qdate","false");
            }
        }
        model.addAttribute("yOrders",list);
        return  "y-order";
    }

    @RequestMapping("/schedule.html")
    public String schedule(HttpSession session, Model model,@RequestParam("liindex")String liindex){
        //获取id
         Users user = (Users) session.getAttribute("user");
         //获取标记
        model.addAttribute("liindex",liindex);
        //房東订单进行中
        model.addAttribute("wOrders",ordersService.fdListOrders(user.getUsers_id()));
        return "schedule";
    }

    @RequestMapping("/scheduleY.html")
    public String scheduleY(HttpSession session, Model model,@RequestParam("liindex")String liindex){
        //获取id
         Users user = (Users) session.getAttribute("user");
        //获取标记
        model.addAttribute("liindex",liindex);
        //房東订单已完成
        model.addAttribute("yOrders",ordersService.fdListOrders1(user.getUsers_id()));
        return "schedule-y";
    }

    //取消订单
    @RequestMapping("/qxOrders.json")
    @ResponseBody
    public Object qxOrders(@RequestParam("orders_id")String orders_id){
        HashMap<String,Object> maps = new HashMap<String,Object>();
        if(ordersService.delOrders(orders_id)>0){
            maps.put("defult","true");
        }else{
            maps.put("defult","false");
        }
        return maps;
    }

    //进入下订单页面
    @RequestMapping("/pay-rent.html/{house_id}")
    public String payRren(@PathVariable("house_id")int house_id,Model model){
        //查询房屋信息
        House house = houseService.houseView(house_id);

        //获得房屋省市区
        System.out.println(house.getCity().toString());
        //区id
        int codeid = house.getCity().getCodeid();
        //区名
        String cityName = house.getCity().getCityName();
        City city = cityService.findCityId(codeid);
        int parentid = city.getParentid();
        System.out.println(parentid+"-------区父id");
        if(parentid!=0){
            city = cityService.findCityId(parentid);
            //市+区
            cityName =  city.getCityName()+cityName;
            //市id
            parentid =  city.getParentid();
            System.out.println(parentid+"-------市父id");
            if(parentid!=0){
                cityName=cityService.findCityId(parentid).getCityName()+cityName;
            }
        }
        System.out.println(cityName);
        //保存房子城市
        model.addAttribute("cityName",cityName);
        //保存房子信息
        model.addAttribute("house",house);
        return "pay-rent";
    }

    //订单价格
    @RequestMapping("xOrders.html")
    public String xOrders(@RequestParam("house_id")int house_id,
                          @RequestParam("day")int day,
                          @RequestParam("startDate")String startDate,
                          @RequestParam("endDate")String endDate,
                          Model model){
        //查询房屋信息
        House house = houseService.houseView(house_id);
        //获得房屋省市区
        System.out.println(house.getCity().toString());
        //区id
        int codeid = house.getCity().getCodeid();
        //区名
        String cityName = house.getCity().getCityName();
        //根据区id查询城市信息
        City city = cityService.findCityId(codeid);
        //获得市id 也就是区的父级id
        int parentid = city.getParentid();
        //判断如果父级id不为0
        if(parentid!=0){
            //查询市信息根据父级id
            city = cityService.findCityId(parentid);
            //市+区
            cityName =  city.getCityName()+cityName;
            //市id
            parentid =  city.getParentid();
            if(parentid!=0){
                cityName=cityService.findCityId(parentid).getCityName()+cityName;
            }
        }
        //获得房子图片
        Fimg fimg = fimgService.listFimg(house_id);
        System.out.println(fimg.getImgname());
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate",endDate);
        model.addAttribute("fimg",fimg.getImgname());
        model.addAttribute("cityName",cityName);
        model.addAttribute("house",house);
        model.addAttribute("day",day);
        return "order";
    }

    //生成订单
    @RequestMapping("/payment.json")
    @ResponseBody
    public Object payment(
                        /*  @RequestParam("house_id")int house_id,
                         @RequestParam("firstday")String firsday,
                         @RequestParam("lastday")String lastday,
                          @RequestParam("price")double price,
                         @RequestParam("fdusers_id")String fdusers_id,
                         @RequestParam("ordersimage")String ordersimage,*/
                        Orders orders,
                         House house,
                         HttpSession session,
                         Model model) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //实例化订单表
      //  Orders orders = new Orders();


        //获得日期的订单日期
        orders.setOrders_id(new Date().getTime());



        //实例化房子表
      //  House house = new House();
        //house.setHouse_id(house_id);

        //房子表id放进订单表
        orders.setHouse(house);
        //下单时间
        orders.setDtime(new Date());

      /*  System.out.println(sdf.parse(firsday));
        orders.setFirstday(sdf.parse(firsday));
        orders.setLastday(sdf.parse(lastday));*/
       // orders.setPrice(price);



        //用户信息获取
        Users user = (Users)session.getAttribute("user");
        //订单表users_id赋值
        orders.setUsers_id(user.getUsers_id());


      //  orders.setFdusers_id(Integer.parseInt(fdusers_id));
       // orders.setOrdersimage(ordersimage);

        //支付状态默认为0 未付
        orders.setZfzt(0);
        System.out.println(orders.toString());
        //hashmap定义
        HashMap<String,Object> maps = new HashMap<String, Object>();
        if(ordersService.addOrders(orders)>0) {
            System.out.println("s");
            //穿orders_id给ajax
            maps.put("orders",orders.getOrders_id());
        }else{
            maps.put("orders","false");
        }
        return maps;
    }

    //付款页面
    @RequestMapping("/payment.html/{orders_id}")
    public String payment(@PathVariable("orders_id")long orders_id,Model model){
        model.addAttribute("orders",ordersService.findOrdersId(orders_id));
        return "payment";
    }

}
